package com.drojian.thread;

import org.junit.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 实现多线程的三种方式
 */
public class Test1 {
    @Test
    public void test1() {
        ThreadMethod1 t1 = new ThreadMethod1();
        ThreadMethod1 t2 = new ThreadMethod1();
        t1.setName("线程1");
        t2.setName("线程2");
        t1.start();
        t2.start();
    }

    @Test
    public void test2() {
        ThreadMethod2 t1 = new ThreadMethod2();
        ThreadMethod2 t2 = new ThreadMethod2();
        Thread thread1 = new Thread(t1);
        Thread thread2 = new Thread(t2);
        thread1.setName("线程1");
        thread2.setName("线程2");
        thread1.start();
        thread2.start();
    }

    @Test
    public void test3() throws ExecutionException, InterruptedException {
        ThreadMethod3 t1 = new ThreadMethod3();
        FutureTask<Integer> future = new FutureTask<Integer>(t1);
        Thread thread = new Thread(future);
        thread.start();
        System.out.println(future.get());

    }
    @Test
    public void test4() {
        SellTicket t1 = new SellTicket();
        SellTicket t2 = new SellTicket();
        SellTicket t3 = new SellTicket();
        t1.setName("线程1");
        t2.setName("线程2");
        t3.setName("线程3");
        t1.start();
        t2.start();
        t3.start();
    }
    @Test
    public void test5() {
        SellTicket2 sellTicket2 = new SellTicket2();
        Thread thread1 = new Thread(sellTicket2);
        Thread thread2 = new Thread(sellTicket2);
        Thread thread3 = new Thread(sellTicket2);
        thread1.setName("线程1");
        thread2.setName("线程2");
        thread3.setName("线程3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
    @Test
    public void test6() {
        LockSellTicket thread1 = new LockSellTicket();
        LockSellTicket thread2 = new LockSellTicket();
        LockSellTicket thread3 = new LockSellTicket();
        thread1.setName("线程1");
        thread2.setName("线程2");
        thread3.setName("线程3");
        thread1.start();
        thread2.start();
        thread3.start();
    }


}
